<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\model\Sms;
use App\Http\model\order;

class OrderController extends BaseController
{
	/**
	 * [等待司机接单时展示信息api]
	 * @Author   孙丽男
	 * @DateTime 2018-05-24
	 * @version  0.8
	 * @param    [订单ID] [int] orderId
	 * @return   [json] $result
	 */
	public function waitReceipt(Request $request){

		$result = array();

		$orderId = $request['orderId'];
		if (!$orderId) {
			
			$result['code'] = 10002;
			$result['msg'] = '参数为空';
			return json_encode($result);

		}
		
		//需查询字段
		$fields = array(
			'order.id',
			'car_id',
			'end',
			'origin',
			'appointment',
			'passenger_name',
			'passenger_phone',
			'passenger_people',
			'order.status',
			'car_series.series_name'
		);

		$orderModel = new order;

		return $orderModel->waitReceiptModel($fields,$orderId);
	}

	/**
	 * [订单进行中api]
	 * @Author   孙丽男
	 * @DateTime 2018-05-24
	 * @param    [订单ID] [int] orderId
	 * @version  0.8
	 * @return   [json]  $result
	 */
	public function orderProcess(Request $request){
		
		$result = array();

		$orderId = $request['orderId'];
		if (!$orderId) {
			
			$result['code'] = 10002;
			$result['msg'] = '参数为空';
			return json_encode($result);

		}

		//需查询字段
		$fields = array(
			'order.id',
			'end',
			'origin',
			'chauffeur.name',
			'chauffeur.phone',
			'chauffeur.picture',
			'series_name',
			'car_number',
			'car_corlor',
			'order.status',
		);
		$orderModel = new order;

		return $orderModel->orderProcessModel($fields,$orderId);
	}

	/**
	 * [服务结束/待支付api]
	 * @Author   孙丽男
	 * @DateTime 2018-05-24
	 * @param    [订单ID] [int] orderId
	 * @version  0.8
	 * @return   [json]   $result
	 */
	public function serverEnd(Request $request){

		$result = array();

		$orderId = $request['orderId'];
		if (!$orderId) {
			
			$result['code'] = 10002;
			$result['msg'] = '参数为空';
			return json_encode($result);

		}

		//需查询字段
		$fields = array(
			'order.id',
			'end',
			'origin',
			'chauffeur.name',
			'chauffeur.picture',
			'series_name',
			'car_number',
			'price',
			'true_price',
			'difprice',
			'car_corlor',
			'order.status',
			'wystatus',
		);
		$orderModel = new order;

		return $orderModel->orderProcessModel($fields,$orderId);
		
	}

	/**
	 * [取消订单api]
	 * @Author   孙丽男
	 * @DateTime 2018-05-24
	 * @param    [订单ID] [int] orderid
	 * @version  0.8
	 * @return   [json]     $result
	 */
	public function cancelOrder(Request $request){

		$result = array();

		$orderId = $request['orderId'];
		if (!$orderId) {
			
			$result['code'] = 10002;
			$result['msg'] = '参数为空';
			return json_encode($result);

		}

		//需查询字段
		$fields = array(
			'order.id',
			'user_id',
			'appointment',
			'order.price',
			'server_item.name',
			'orders_phone',
			'sum_money'
		);
		$orderModel = new order;

		$orderInfoArr = $orderModel->orderInfo($fields,$orderId);

		if ($orderInfoArr['code'] == 1004) {
			
			return json_encode($orderInfoArr);
		}	
		date_default_timezone_set('PRC');

		//当前时间
		$datetime = strtotime(date('Y-m-d H:i:s'));
		//预约时间
		$appointment = $orderInfoArr['data']['appointment'];
		//时间差/小时
		$diftime = round(($appointment - $datetime)/3600,2);
		
		$uid = $orderInfoArr['data']['user_id'];
		$price = $orderInfoArr['data']['price'];
		$sum_money = $orderInfoArr['data']['sum_money'];
		$type = $orderInfoArr['data']['name'];
		$phone = $orderInfoArr['data']['orders_phone'];
		$percentage = '';

		/**
		 * 判断时间差，根据时间差扣款，大于两小时不扣款，
		 * 小于等于两小时并且大于一小时扣预付款80%，小于等于一小时扣50%，预约订单开始之后扣去全部预约款
		 */	
		if ( $diftime > 2 ) {
			
			//差值
			$difprice = $price ;
			$percentage = 0;
		}elseif ($diftime <= 2 && $diftime > 1) {
			
			$difprice = $price - $price*0.5;
			$percentage = 50;

		}elseif($diftime < 1 && $diftime > 0){

			$difprice = $price - $price*0.8;
			$percentage = 80;

		}else{

			$res = $orderModel->updateStatusModel($orderId);

			if ($res['code'] == 1000) {
				//扣除全部预付款，直接发送订单取消成功短信
				$percentage = 100;
				$sendResult = $this->cancelSms($percentage,$appointment,$phone,$type);

				if ($sendResult == 1000) {
					
					$result['code'] = 1000;
					$result['msg'] = '取消成功';
					return json_encode($result);

				}else{

					$result['code'] = 1005;
					$result['msg'] = '短信发送失败';
					return json_encode($result);
				}
			}else{
				return json_encode($res);
			}
		}

		//将扣除之后的预付款返还给用户并修改订单状态
		$money = $sum_money + $difprice ;

		$updateResult = $orderModel->updateModel($money,$uid,$orderId);
		
		if ($updateResult['code'] == 1000) {
			
			$sendResult = $this->cancelSms($percentage,$appointment,$phone,$type);
			
			if ($sendResult == 1000) {
				
				$result['code'] = 1000;
				$result['msg'] = '取消成功';
				return json_encode($result);

			}else{

				$result['code'] = 1005;
				$result['msg'] = '短信发送失败';
				return json_encode($result);
			}

		}else{

			return json_encode($updateResult);
		}

	}

	/**
	 * [取消订单成功发送短信api]
	 * @Author   孙丽男
	 * @DateTime 2018-05-24
	 * @param    [订单ID] [int] orderid
	 * @version  0.8
	 * @return   [json]     $result
	 */
	protected function cancelSms($percentage,$appointment,$phone,$type){

		$date = date('Y-m-d H:i:s',$appointment);
		$adress = 'https://b.shidaichuxing.com/api/setorder/orderlist';

		$msg = '【时代出行】您预订'.$date.'的'.$type.'订单已取消，本次取消扣除预估费用'.$percentage.'%。查看详情请点击'.$adress.'或搜索下载APP时代出行，感谢您的使用！';
		
		$code = new Sms();
	    $re = $code -> sendSMS($phone,$msg); 
	    $data = json_decode($re,true);
  
	    if($data['error'] == 0){
	        return 1000;
        }else{
            return 1005;
        }

	}
}